finp$="mammagamma.mid"
if argc>2 then:finp$=argv(2):end if

shell("midicsv "+finp$+" > _t_.csv")

function locstrinfo$(a$,b$,c$)
  adra=1:adrb=1:blngt=len(b$):d$=""
  while adrb<=blngt
    if mid$ (a$,adra,1)=mid$ (b$,adrb,1) Then:adrb+=1:End If
    adra+=1:end while
  while mid$(a$,adra,1)<>left$(c$,1):d$=d$+mid$(a$,adra,1):adra+=1:end while
 locstrinfo$=d$
  end function

endtrack=0
headerdivision=480
'timesignaturenum=4

dim cp[50]
cp[0]=0xFF0000
cp[1]=0x00FF00
cp[2]=0x0000FF
cp[3]=0xFFFF00
cp[4]=0x00FFFF
cp[4]=0xFF00FF
cp[6]=0xFF7F00
cp[7]=0x00FF7F
cp[8]=0x7F00FF
cp[9]=0xFF007F
cp[10]=0x7FFF00
cp[11]=0x007FFF

shell("cp _t_.csv _tmp1_.csv")
shell("touch _info_.csv")
shell("cat _tmp1_.csv | grep Header >> _info_.csv")
shell("cat _tmp1_.csv | grep Time_signature >> _info_.csv")
shell("cat _tmp1_.csv | grep Tempo >> _info_.csv")
shell("cat _tmp1_.csv | grep End_track >> _info_.csv")
shell("rm _tmp1_.csv")

open "_info_.csv" for input as #1
while eof(1)=0:
  file input #1,txi$
  txi$=","+txi$+",,,,,"
  if lcase$(trim$(locstrinfo$(txi$,",,,",",")))="end_track" then
    endtrackr=int(val(trim$(locstrinfo$(txi$,",,",","))))
    if endtrackr>endtrack then:endtrack=endtrackr:end if
    end if
  if lcase$(trim$(locstrinfo$(txi$,",,,",",")))="header" then
    headerdivision=int(val(trim$(locstrinfo$(txi$,",,,,,,",","))))
    end if
  end while
close #1

xsz=endtrack/(headerdivision/4)

setdisplay(xsz,96,32,1):paper(0xFFFFFF):ink(0x000000):pen(0):cls

open "_t_.csv" for input as #1
while eof(1)=0:
  file input #1,txi$
  txi$=","+txi$+",,,,,"
  if lcase$(trim$(locstrinfo$(txi$,",,,",",")))="note_on_c" then
    x=int(val(trim$(locstrinfo$(txi$,",,",",")))/(headerdivision/2))*2
    y=val(trim$(locstrinfo$(txi$,",,,,,",",")))
    c=val(trim$(locstrinfo$(txi$,",,,,",",")))
    print x
    print y
    ink(cp[c])
    dot(x,95-y)
    print
    end if
  if lcase$(trim$(locstrinfo$(txi$,",,,",",")))="note_off_c" then
    x=int(val(trim$(locstrinfo$(txi$,",,",",")))/(headerdivision/2))*2
    y=val(trim$(locstrinfo$(txi$,",,,,,",",")))
    c=val(trim$(locstrinfo$(txi$,",,,,",",")))
    print x
    print y
    ink(0xAAAAAA)
    dot(x+1,95-y)
    print
    end if
  end while
close #1

'for y=0 to 95
'  curc=0xAAAAAA
'  for x=0 to xsz-1
'    u=point(x,y)
'    if u<>0xFFFFFF then:ink(u):end if
'    if u=0xFFFFFF then:dot(x,y):end if
'    next:next
'for y=0 to 95
'  for x=0 to xsz-1
'    u=point(x,y)
'    if u=0xAAAAAA then:ink(0xFFFFFF):dot(x,y):end if
'    next:next



grab(1,0,0,xsz,96):saveimage("_.bmp",1):shell("convert _.bmp "+finp$+".png && rm _.bmp")

'waitkey



